package com.qcby.service.impl;

import com.qcby.common.LessonPlan.ShowByPage;
import com.qcby.dao.LessonPlanDao;
import com.qcby.entity.LessonPlan;
import com.qcby.entity.Result;
import com.qcby.service.DeepSeekService;
import com.qcby.service.LessonPlanService;
import com.qcby.util.WordWriter;
import org.json.JSONObject;
import org.omg.CORBA.StringHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class LessonPlanServiceImpl implements LessonPlanService {

    @Autowired
    private LessonPlanDao lessonPlanDao;
    @Autowired
    private DeepSeekService deepSeekService;
    @Autowired
    private WordWriter wordWriter;

    @Override
    public Result<List<LessonPlan>> showLessonPlanByPage(ShowByPage pageInfo) {
//        System.out.println("-------service-------");
//        System.out.println(pageInfo.toString());
        pageInfo.setOffset((pageInfo.getPage() - 1) * pageInfo.getPageSize());
        return Result.success(lessonPlanDao.showLessonPlanByPage(pageInfo));
    }

    @Override
    public Result<Integer> getLessonPlanCount(ShowByPage pageInfo) {
        return Result.success(lessonPlanDao.getLessonPlanCount(pageInfo));
    }

    @Override
    public Result<LessonPlan> addLessonPlan(LessonPlan info) throws Exception {
        String requestBody = "请你帮我写一份教案，教案中的包括：教案的名称"+info.getName() +"教案的备注"+ info.getNote()+"教案的目标"+ info.getTarget()+"教案的创建人"+info.getCreator()+"教案的创建时间"+info.getCreatetime()+"教案的课时"+info.getTime()+"教案的难度是"+info.getDifficulty()+"教案的科目是"+info.getSubject()+"等信息，帮我完成这份叫教案200字左右";
        String AIresult = deepSeekService.getJson(requestBody);
        JSONObject jsonObject = new JSONObject(AIresult);
        String res = jsonObject.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");
        String url = wordWriter.uploadDoc(res, info.getName() + ".docx");
        info.setWord(url);
        info.setContent(res);
        if(lessonPlanDao.addLessonPlan(info) == 1){
            return Result.success(info);
        }else{
            return Result.error("添加教案信息出错，请联系管理员!");
        }
    }

    @Override
    public Result<Integer> deleteLessonPlan(int id) {
        if(lessonPlanDao.deleteLessonPlan(id) == 1){
            return Result.success(id);
        } else{
            return Result.error("删除教案信息出错，请联系管理员！");
        }
    }

    @Override
    public Result<LessonPlan> showLessonPlanDetail(int id) {
        return Result.success(lessonPlanDao.showLessonPlanDetail(id));
    }

    @Override
    public Result<List<LessonPlan>> fuzzyQueryLessonPlanByPage(ShowByPage info) {
        info.setOffset(info.getPageSize()*(info.getPage()-1));
        return Result.success(lessonPlanDao.fuzzyQueryLessonPlanByPage(info));
    }

    @Override
    public Result<Integer> getLessonPlanCountWhenSearch(ShowByPage info) {
        return Result.success(lessonPlanDao.getLessonPlanCountWhenSearch(info));
    }

    @Override
    public Result getLessonPlanByTeacherName(Integer id) {
        return Result.success(lessonPlanDao.getLessonPlanByTeacherName(id));
    }
}
